const pageStack = getCurrentPages()
Page({
  onLoad(){
    const app=getApp()
    console.log(app);
    console.log(pageStack[0]);
    this.setData({...app.userProfile})
  },
 getUserNickname(ev){
  this.updateNickname(ev.detail.value)
 },
 async updateNickname(nickName){
  if(nickName==='') return
  const {code}=await wx.http.put('/userInfo',{nickName})
  if(code!==10000) return wx.utils.toast('更新昵称失败！')
  pageStack[0].setData({nickName})
  const app = getApp()
  app.userProfile.nickName = nickName
 },
 getUseravatar(ev){
this.updateAvatar(ev.detail.avatarUrl)
 },
 updateAvatar(avatar){
  wx.uploadFile({
    // 接口地址
    url: wx.http.baseURL + '/upload',
    // 待上传的文件路径
    filePath: avatar,
    name: 'file',
    header: {
      // 用户登录状态
      Authorization: 'Bearer ' + getApp().token
    },
    formData: {type: 'avatar'},
    success: (result) => {
      const data = JSON.parse(result.data)
      if(data.code !== 10000) return wx.utils.toast('上传头像失败!')
      pageStack[0].setData({avatar: data.data.url})
      const app = getApp()
      app.userProfile.avatar = data.data.url
        }
       
  })
 
 }
})